package com.codeyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codeyang.domain.IndexImg;
import com.codeyang.service.IndexImgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 描述: TO-删除标志-DO
 *
 * @author CodeYang_Site
 * @version 2021/5/26 14:01
 */
@Api("轮播图管理接口")
@RestController
@RequestMapping("admin/indexImg")
public class IndexImgController {


    @Autowired
    private IndexImgService indexImgService;


    @GetMapping("page")
    @ApiOperation("分页轮播图查询")
    @PreAuthorize("hasAuthority('admin:indexImg:page')")
    public ResponseEntity<Page<IndexImg>> page(Page<IndexImg> page, IndexImg indexImg) {
        Page<IndexImg> imgPage = indexImgService.page(page, new LambdaQueryWrapper<IndexImg>()
                .eq(!ObjectUtils.isEmpty(indexImg.getStatus()), IndexImg::getStatus, indexImg.getStatus())
                .orderByDesc(IndexImg::getSeq)
        );
        return ResponseEntity.ok(imgPage);
    }


    @PostMapping
    @ApiOperation("新增轮播图")
    @PreAuthorize("hasAuthority('admin:indexImg:page')")
    public ResponseEntity<Void> add(@RequestBody IndexImg indexImg) {
        boolean save = indexImgService.save(indexImg);
        return ResponseEntity.ok().build();
    }


    @GetMapping("info/{id}")
    @ApiOperation("根据轮播图id显示轮播图信息")
    @PreAuthorize("hasAuthority('admin:indexImg:page')")
    public ResponseEntity<IndexImg> info(@PathVariable("id") Long indexImgId) {

        IndexImg indexImg = indexImgService.findIndexImgById(indexImgId);
        return ResponseEntity.ok(indexImg);
    }


    @PutMapping()
    @ApiOperation("修改轮播图")
    @PreAuthorize("hasAuthority('admin:indexImg:page')")
    public ResponseEntity<Void> update(@RequestBody IndexImg indexImg) {
        indexImgService.updateById(indexImg);
        return ResponseEntity.ok().build();
    }
    //----------



}
